/**
 * 
 */
package com.tomagency.cyberlab.client.ui.renderer;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.tomagency.cyberlab.client.CssGlobalStyleNames;
import com.tomagency.cyberlab.client.events.BelegEvent;
import com.tomagency.cyberlab.client.events.BelegEventImpl;
import com.tomagency.cyberlab.client.events.ObjectEvent;
import com.tomagency.cyberlab.client.events.ObjectEventImpl;
import com.tomagency.cyberlab.client.ui.ObjectListenView.CellRenderer;
import com.tomagency.cyberlab.client.ui.ObjectListenView.ObjectEventHandler;
import com.tomagency.cyberlab.client.ui.ObjectListenView.RowType;

import de.gammadata.gwt.proxy.PersistenceBeanListProxy;

/**
 * @author gfr
 *
 */
public class CollapseCellRenderer implements CellRenderer<ObjectEvent> {

	@Override
	public int getColSpan() {
		return 1;
	}

	@Override
	public Widget getColumnWidget(RowType rowType,
			ObjectEventHandler<ObjectEvent> handler, Object dataObj) {
		Label collapsLabel = new Label("-");
		collapsLabel.setStyleName(CssGlobalStyleNames.EINTRAG_COLLAPSE);
		int id = dataObj instanceof PersistenceBeanListProxy ? ((PersistenceBeanListProxy)dataObj).getId() : 0;
		collapsLabel.addClickHandler(new CollapseClickHandler(id, handler));
		return collapsLabel;
	}
	
	class CollapseClickHandler implements ClickHandler{
		
		int objectId=0;
		ObjectEventHandler<ObjectEvent> handler;
		
		CollapseClickHandler (int objectId,ObjectEventHandler<ObjectEvent> handler){
			this.objectId=objectId;
			this.handler=handler;			
		}

		@Override
		public void onClick(ClickEvent event) {
			event.stopPropagation();
			/*
			 * Object Handling ist an den Persenter.Objecthandler delegiert
			 */
			ObjectEvent oe = new ObjectEventImpl();
			oe.setEventType(ObjectEvent.ONCOLLAPSE);
			oe.setObjectId(objectId);
			handler.onObjectCollapseEvent(oe);
			
		}
		
	}

}
